package GUI;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.util.Date;

import XMLForms.*;
import XMLTranslator.*;
import UTGBusiness.*;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class submitButton extends JPanel implements ActionListener{
    private JButton button;
    private Controller control;
    public submitButton(Controller c)
    {
        this.control = c;
        button = new JButton("Execute Trade");
        button.addActionListener(this);

        JPanel panel = new JPanel(new GridBagLayout());
        //c.fill = GridBagConstraints.HORIZONTAL;
        panel.add(button);
        add(panel, BorderLayout.LINE_START);
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        String messageString;
        TTRM ttrm = new TTRM(control.getBuySell(), "", control.getTraderID(),
                Integer.valueOf(control.getAmount()),
                control.getStockID(), control.getOfferTime(), new Date(), 
                Double.valueOf(control.getPrice())); 
        Object response = new UTG().execute(ttrm);
        if (response instanceof TTCM)        //trade complete
        {           
            messageString = "Acceptor ID: " + ((TTCM) response).getAcceptorID()
                    + "\nFirm Sequence Number: " +
                ((TTCM) response).getFirmSequencNumber();
            JOptionPane.showMessageDialog(null,messageString ,((TTCM) response)
                    .getTradeState() ,JOptionPane.INFORMATION_MESSAGE);
           
        }else if (response instanceof TVFM)
        {
            messageString = ((TVFM) response).getErrorMessage();
            JOptionPane.showMessageDialog(null,messageString , 
                    "Trade Validation Failed",JOptionPane.ERROR_MESSAGE);
        }else
            System.out.println("We have an error");
        //System.out.println(ttrm);
    }

}
